﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Configuration;
using System.Net;
using System.Xml;
using System.Threading;
using System.Drawing;
using System.IO;

namespace BingImages
{
    class FluxRSS
    {
        // ------------ DEBUT INITIALISATION VARIABLES ------------ //
        string confFlux = ConfigurationManager.AppSettings["flux"];

        static Thread fluxThread;
        int time = 0;
        //Image tmpimg = null;
        // ------------ FIN INITIALISATION VARIABLES ------------ //

        public FluxRSS()
        {
            fluxThread = new Thread(readFlux);
            fluxThread.IsBackground = true;
            fluxThread.Start();
        }

        public void readFlux()
        {
            String link;

            if(!string.IsNullOrEmpty(confFlux))
            {
                link = confFlux;
            }
            else
            {
                link = "Vide";
            }

            XmlTextReader rssReader = null;
            XmlReader itemReader = null;
            int itemCount = 0;

            try
            {
                while (confFlux == link && time < 300)
                {
                    Thread.Sleep(1000);

                    WebRequest rssFeed = WebRequest.Create("http://feeds.feedburner.com/bingimages");

                    rssReader = new XmlTextReader(rssFeed.GetResponse().GetResponseStream());

                    rssReader.ReadStartElement();

                    while (rssReader.Read())
                    {
                        itemReader = rssReader.ReadSubtree();
                        while (itemReader.Read())
                        {
                            if (itemReader.IsStartElement())
                            {
                                if ("link" == itemReader.LocalName)
                                {
                                    if (itemCount == 3)
                                    {
                                        link = itemReader.ReadString();

                                        time++;
                                    }

                                    itemCount = itemCount + 1;
                                }
                            }
                            if (itemCount >= 4)
                            {
                                break;
                            }
                        }
                        if (itemCount >= 4)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                link = exp.Message;
            }
            finally
            {
                if (itemReader != null && time != 300)
                {
                    itemReader.Close();

                    // ***** DEBUT PARAMETRE MODIFIE : FLUX ***** //
                    Config maConfig = new Config("flux", link);
                    // ***** FIN PARAMETRE MODIFIE : FLUX ***** //                    
                }
            }
        }
    }
}
